home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Quad.cc < prev    next >
C/C++ Source or Header  |  1996-06-06  |  4KB  |  165 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "Quad.h"
  32. #include "f77-fcn.h"
  33. #include "lo-error.h"
  34. #include "sun-utils.h"
  35.  
  36. static integrand_fcn user_fcn;
  37.  
  38. // XXX FIXME XXX -- would be nice to not have to have this global
  39. // variable.
  40. // Nonzero means an error occurred in the calculation of the integrand
  41. // function, and the user wants us to quit.
  42. int quad_integration_error = 0;
  43.  
  44. extern "C"
  45. {
  46.   int F77_FCN (dqagp, DQAGP) (const double (*)(double*, int&),
  47.                   const double&, const double&,
  48.                   const int&, const double*,
  49.                   const double&, const double&, double&,
  50.                   double&, int&, int&, const int&,
  51.                   const int&, int&, int*, double*);
  52.  
  53.   int F77_FCN (dqagi, DQAGI) (const double (*)(double*, int&),
  54.                   const double&, const int&,
  55.                   const double&, const double&, double&,
  56.                   double&, int&, int&, const int&,
  57.                   const int&, int&, int*, double*); 
  58. }
  59.  
  60. static double
  61. user_function (double *x, int& ierr)
  62. {
  63. #if defined (sun) && defined (__GNUC__)
  64.   double xx = access_double (x);
  65. #else
  66.   double xx = *x;
  67. #endif
  68.  
  69.   quad_integration_error = 0;
  70.  
  71.   double retval = (*user_fcn) (xx);
  72.  
  73.   if (quad_integration_error)
  74.     ierr = -1;
  75.  
  76.   return retval;
  77. }
  78.  
  79. double
  80. DefQuad::integrate (int& ier, int& neval, double& abserr)
  81. {
  82.   int npts = singularities.capacity () + 2;
  83.   double *points = singularities.fortran_vec ();
  84.   double result = 0.0;
  85.  
  86.   int leniw = 183*npts - 122;
  87.   Array<int> iwork (leniw);
  88.   int *piwork = iwork.fortran_vec ();
  89.  
  90.   int lenw = 2*leniw - npts;
  91.   Array<double> work (lenw);
  92.   double *pwork = work.fortran_vec ();
  93.  
  94.   user_fcn = f;
  95.   int last;
  96.  
  97.   double abs_tol = absolute_tolerance ();
  98.   double rel_tol = relative_tolerance ();
  99.  
  100.   F77_XFCN (dqagp, DQAGP, (user_function, lower_limit, upper_limit,
  101.                npts, points, abs_tol, rel_tol, result,
  102.                abserr, neval, ier, leniw, lenw, last,
  103.                piwork, pwork));
  104.  
  105.   if (f77_exception_encountered)
  106.     (*current_liboctave_error_handler) ("unrecoverable error in dqagp");
  107.  
  108.   return result;
  109. }
  110.  
  111. double
  112. IndefQuad::integrate (int& ier, int& neval, double& abserr)
  113. {
  114.   double result = 0.0;
  115.  
  116.   int leniw = 128;
  117.   Array<int> iwork (leniw);
  118.   int *piwork = iwork.fortran_vec ();
  119.  
  120.   int lenw = 8*leniw;
  121.   Array<double> work (lenw);
  122.   double *pwork = work.fortran_vec ();
  123.  
  124.   user_fcn = f;
  125.   int last;
  126.  
  127.   int inf;
  128.   switch (type)
  129.     {
  130.     case bound_to_inf:
  131.       inf = 1;
  132.       break;
  133.  
  134.     case neg_inf_to_bound:
  135.       inf = -1;
  136.       break;
  137.  
  138.     case doubly_infinite:
  139.       inf = 2;
  140.       break;
  141.  
  142.     default:
  143.       assert (0);
  144.       break;
  145.     }
  146.  
  147.   double abs_tol = absolute_tolerance ();
  148.   double rel_tol = relative_tolerance ();
  149.  
  150.   F77_XFCN (dqagi, DQAGI, (user_function, bound, inf, abs_tol, rel_tol,
  151.                result, abserr, neval, ier, leniw, lenw,
  152.                last, piwork, pwork));
  153.  
  154.   if (f77_exception_encountered)
  155.     (*current_liboctave_error_handler) ("unrecoverable error in dqagi");
  156.  
  157.   return result;
  158. }
  159.  
  160. /*
  161. ;;; Local Variables: ***
  162. ;;; mode: C++ ***
  163. ;;; End: ***
  164. */
  165.